//
//  YFSearchView.swift
//  微博
//
//  Created by 余仿 on 16/4/6.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

class YFSearchView: UIView {


    @IBOutlet weak var textField: UITextField!

    @IBOutlet weak var trailing: NSLayoutConstraint!

    @IBOutlet weak var cancel: UIButton!

    override func awakeFromNib() {

        self.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width - 50, height: self.bounds.height)

        let leftSearchView = UIButton()

        //leftSearchView.backgroundColor = UIColor.redColor()

        leftSearchView.setImage(UIImage(named: "searchbar_textfield_search_icon"), forState: .Normal)

        leftSearchView.frame = CGRectMake(0, 0, self.bounds.height, self.bounds.height)

//        leftSearchView.contentMode = .Center

        textField?.leftView = leftSearchView

        textField?.leftViewMode = .Always

    }


    
    class func loadSearchView () -> YFSearchView {

        let searchView = NSBundle.mainBundle().loadNibNamed("YFSearchView", owner: nil, options: nil).last as! YFSearchView

        return searchView
    }


    @IBAction func beginEditing(sender: UITextField) {

        print("改变文本框frame,以动画的方式")

        trailing.constant = cancel.bounds.width

        UIView.animateWithDuration(1) { () -> Void in

            self.layoutIfNeeded()

        }
    }

    @IBAction func cancelDidClick(sender: UIButton) {
        print("取消被点击了,键盘回收,文本框复位")
        trailing.constant = 0
        textField.resignFirstResponder()

        UIView.animateWithDuration(1) { () -> Void in

            self.layoutIfNeeded()
        }
    }

}
